<script setup>
import {ref} from "vue";
import * as Auth from "@/utils/auth.js";
import {loginByEmail, loginByphone} from "@/api/user.js";
import {useRoute, useRouter} from "vue-router";
import {showToast} from "vant";

const loginForm = ref({name: '11111111111', password: '111111'})
const router = useRouter();
const route = useRoute();
const params_define = defineProps({
  redirect: String
});

function onSubmit() {
  //检查登录名是否包含‘@’符号
  if (loginForm.value.name.indexOf('@') > -1) {
    // 如果包含 '@'，则认为是邮箱登录
    let params = {
      // 提取邮箱
      email: loginForm.value.name,
      // 提取密码
      pwd: loginForm.value.password
    };
    // 调用登录ByEmail的函数，传递提取的邮箱和密码参数
    loginByEmail(params).then(result => {
      console.log(result);
      // 检查登录结果中的返回码
      if (result.data.code === 500) {
        showToast(result.data.msg);
        return;
      }
      // 如果登录成功，保存 token 到本地
      Auth.setTokenLocal(result.data.token.tokenValue);
      // 保存用户信息到本地
      Auth.setUserLocal(result.data.data);
      // 弹出提示，显示登录成功
      showToast('登录成功')
      // 获取重定向路径
      let toPath = route.params.redirect;
      console.log(toPath, 'toPath')
      // 延时1秒后跳转到指定路径
      setTimeout(() => {
        router.push({name: toPath});
      }, 1000);

    });
  } else {
    let params = {
      phone: loginForm.value.name,
      pwd: loginForm.value.password
    };
    loginByphone(params).then(result => {
      console.log(result);
      if (result.data.code === 500) {
        showToast(result.data.msg);
        return;
      }
      Auth.setTokenLocal(result.data.token.tokenValue);
      console.log(result.data.token.tokenValue)
      Auth.setUserLocal(result.data.data);
      showToast('登录成功')
      let toPath = route.params.redirect;
      console.log(toPath, 'toPath')
      setTimeout(() => {
        router.push({name: toPath});
      }, 1000);
    });
  }
}
</script>

<template>
  <div class="thePage">
    <van-nav-bar
        title="登录"
        left-text="返回"
        left-arrow
        @click-left="$router.push('/user')">
    </van-nav-bar>
    <van-row type="flex" justify="center">
      <van-image height="100" width="100" src="/vite.svg"></van-image>
    </van-row>
    <van-cell-group inset :border="true">
      <van-form @submit="onSubmit">
        <van-field v-model="loginForm.name" name="name" label="用户名" placeholder="用户名"
                   :rules="[{ required: true, message: '请填写用户名' }]"/>
        <van-field v-model="loginForm.password" type="password" name="password" label="密码" placeholder="密码"
                   :rules="[{ required: true, message: '请填写密码' }]"/>
        <div style="margin: 16px;">
          <van-button block type="primary" size="large" native-type="submit">提交</van-button>
        </div>
      </van-form>

      <div>
        <van-button class="goto" type="info" size="large" @click="$router.push('/register')">如果没有账号,请立即注册</van-button>
      </div>
    </van-cell-group>
  </div>
</template>

<style scoped>
.thePage {
  background-color: #f6f6f6;
  height: 93vh;
}

.goto {
  display: block;
  margin-top: 25px;
  text-align: center;
}
</style>